package qc.module.platform.api.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import qc.common.core.exception.QCPromptException;
import qc.common.core.unify.QCUnifyReturnValue;
import qc.module.platform.dto.param.group.ParamGroupDto;
import qc.module.platform.dto.param.value.ObjectParamValuesDto;
import qc.module.platform.dto.param.value.ParamValuesGetConditionDto;
import qc.module.platform.dto.project.ProjectDto;
import qc.module.platform.dto.project.ProjectParamValueConditionDto;
import qc.module.platform.dto.project.ProjectQueryConditionDto;
import qc.module.platform.dto.project.ProjectUserRelateDto;
import qc.module.platform.dto.user.UserQueryConditionDto;
import qc.module.platform.dto.user.UserSimpleInfoDto;
import qc.module.platform.service.*;

import java.sql.SQLException;
import java.util.List;

/**
 * ProjectController
 *
 * @author QuCheng Tech
 * @create 2023/1/9
 */
@RestController
@RequestMapping("/project")
public class ProjectController {
    private ProjectService projectService;
    private UserProjectService userProjectService;
    private UserService userService;
    private ParamService paramService;
    private ParamValueService paramValueService;

    @Autowired
    public void setProjectService(ProjectService projectService) {
        this.projectService = projectService;
    }

    @Autowired
    public void setUserProjectService(UserProjectService userProjectService) {
        this.userProjectService = userProjectService;
    }

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @Autowired
    public void setParamService(ParamService paramService) {
        this.paramService = paramService;
    }

    @Autowired
    public void setParamValueService(ParamValueService paramValueService) {
        this.paramValueService = paramValueService;
    }

    //是否为逻辑删除，为true表示逻辑删除--修改记录标记，为false表示物理删除--从数据库表中删除记录
    private static final boolean isLogicDelete = false;

    /**
     * 新增项目
     *
     * @param dto 项目
     * @return 成功返回null
     * @author QuCheng Tech
     * @since 2023/6/26
     */
    @RequestMapping(value = "/add", method = {RequestMethod.POST})
    public String add(@RequestBody ProjectDto dto) {
        return projectService.add(dto);
    }

    /**
     * 修改项目
     *
     * @param dto 项目
     * @return 成功返回null
     * @author QuCheng Tech
     * @since 2023/6/26
     */
    @RequestMapping(value = "/update", method = {RequestMethod.PUT, RequestMethod.POST})
    public String update(@RequestBody ProjectDto dto) {
        return projectService.update(dto);
    }

    /**
     * 删除项目，物理删除
     *
     * @param id 项目编码
     * @return 成功返回null
     * @author QuCheng Tech
     * @since 2023/6/27
     */
    @RequestMapping(value = "/delete", method = {RequestMethod.DELETE})
    public String deleteByParam(@RequestParam String id) {
        return projectService.delete(id, isLogicDelete);
    }

    /**
     * 删除项目，物理删除
     *
     * @param id 项目编码
     * @return 成功返回null
     * @author QuCheng Tech
     * @since 2023/6/27
     */
    @RequestMapping(value = "/delete/{id}", method = {RequestMethod.DELETE})
    public String deleteByPath(@PathVariable String id) {
        return projectService.delete(id, isLogicDelete);
    }

    /**
     * 获取指定项目信息，不对项目状态进行判断
     *
     * @param id 项目编码
     * @return 项目信息
     * @author QuCheng Tech
     * @since 2023/6/27
     */
    @RequestMapping(value = "/get", method = {RequestMethod.GET})
    public ProjectDto getByParam(@RequestParam String id) throws QCPromptException {
        return projectService.get(id);
    }

    /**
     * 获取指定项目信息，不对项目状态进行判断
     *
     * @param id 项目编码
     * @return 项目信息
     * @author QuCheng Tech
     * @since 2023/6/27
     */
    @RequestMapping(value = "/get/{id}", method = {RequestMethod.GET})
    public ProjectDto getByPath(@PathVariable String id) throws QCPromptException {
        return projectService.get(id);
    }

    /**
     * 根据关键字查询，关键字可以为项目编码、项目名称、项目标题(完整、简要标题)，没有则查询全部
     *
     * @param dto ProjectQueryConditionDto
     * @return 项目信息集合
     * @author QuCheng Tech
     * @since 2023/2/8
     */
    @RequestMapping(value = "/query", method = {RequestMethod.GET, RequestMethod.POST})
    public List<ProjectDto> query(@RequestBody ProjectQueryConditionDto dto) {
        return projectService.query(dto, false);
    }

    /**
     * 根据项目编码获取关联的用户简要信息集合
     *
     * @param id 项目编码
     * @return 关联用户简单信息列表
     * @author QuCheng Tech
     * @since 2023/6/27
     */
    @RequestMapping(value = "/getusers", method = {RequestMethod.GET})
    public List<UserSimpleInfoDto> getProjectUsersByParam(@RequestParam String id) throws QCPromptException {
        UserQueryConditionDto condition = new UserQueryConditionDto();
        condition.setProject(id);

        return userService.queryProjectUserSimple(condition);
    }

    /**
     * 获取项目关联用户
     *
     * @param id 项目编码
     * @return 关联用户简单信息列表
     * @author QuCheng Tech
     * @since 2023/6/27
     */
    @RequestMapping(value = "/getusers/{id}", method = {RequestMethod.GET})
    public List<UserSimpleInfoDto> getProjectUsersByPath(@PathVariable String id) throws QCPromptException {
        UserQueryConditionDto condition = new UserQueryConditionDto();
        condition.setProject(id);

        return userService.queryProjectUserSimple(condition);
    }

    /**
     * 添加项目关联用户，可关联多个用户
     *
     * @param dto 项目关联用户信息
     * @return 成功返回null
     * @author QuCheng Tech
     * @since 2023/6/27
     */
    @RequestMapping(value = "/addusers", method = {RequestMethod.POST})
    public String addProjectUsers(@RequestBody ProjectUserRelateDto dto) {
        if (dto == null)
            return QCUnifyReturnValue.Warn("设置信息不能为空");

        String projectCode = dto.getProject();
        int[] userIds = dto.getUids();

        if (StringUtils.isBlank(projectCode))
            return QCUnifyReturnValue.Warn("设置的项目编码不能为空");
        if (userIds == null || userIds.length < 0x1)
            return QCUnifyReturnValue.Warn("设置的用户信息不能为空");

        if (!projectService.hasCodeExist(projectCode))
            return QCUnifyReturnValue.Warn("设置的项目编码不存在");

        userProjectService.add(projectCode, userIds);

        return QCUnifyReturnValue.Success();
    }

    /**
     * 删除项目指定用户关联关系，可选择多个用户
     *
     * @param dto 项目关联用户信息
     * @return 成功返回null
     * @author QuCheng Tech
     * @since 2023/6/27
     */
    @RequestMapping(value = "/deleteusers", method = {RequestMethod.DELETE})
    public String deleteProjectUsers(@RequestBody ProjectUserRelateDto dto) {
        if (dto == null)
            return QCUnifyReturnValue.Warn("设置信息不能为空");

        String projectCode = dto.getProject();
        int[] userIds = dto.getUids();

        if (StringUtils.isBlank(projectCode))
            return QCUnifyReturnValue.Warn("设置的项目编码不能为空");
        if (userIds == null || userIds.length < 0x1)
            return QCUnifyReturnValue.Warn("设置的用户信息不能为空");

        if (!projectService.hasCodeExist(projectCode))
            return QCUnifyReturnValue.Warn("设置的项目编码不存在");

        userProjectService.delete(projectCode, userIds);

        return QCUnifyReturnValue.Success();
    }

    /***
     * 根据指定的项目编码和参数分组编码获取参数值信息
     *
     * @param condition 指定的项目编码和参数分组编码
     * @return qc.module.platform.dto.param.value.ObjectParamValuesDto
     * @author QuCheng Tech
     * @since 2024/3/4
     */
    @RequestMapping(value = "/paramvalue", method = {RequestMethod.GET, RequestMethod.POST})
    public ObjectParamValuesDto getParamValue(@RequestBody ProjectParamValueConditionDto condition) throws QCPromptException, SQLException {
        //判断输入信息和条件
        if (condition == null)
            throw new QCPromptException("获取项目参数信息的条件对象不能为空");
        if (StringUtils.isBlank(condition.getProjectcode()))
            throw new QCPromptException("获取项目参数信息的项目编码不能为空");
        if (StringUtils.isBlank(condition.getGroupcode()))
            throw new QCPromptException("获取项目参数信息的参数分组编码不能为空");

        ParamValuesGetConditionDto getParamValuesCondition = new ParamValuesGetConditionDto();
        //从传入的参数分组编码获取参数分组信息，使用参数分组信息中的数据库表编码和节点名称
        ParamGroupDto paramGroup = paramService.getGroup(condition.getGroupcode());
        if (paramGroup == null)
            throw new QCPromptException("获取项目参数信息的参数分组编码获取不到有效的参数分组信息");

        getParamValuesCondition.setTablecode(paramGroup.getTablecode());
        getParamValuesCondition.setObjectids(condition.getProjectcode());
        getParamValuesCondition.setParamnodes(paramGroup.getNodes());

        List<ObjectParamValuesDto> paramValues = paramValueService.getValues(getParamValuesCondition);
        if (paramValues != null && paramValues.size() > 0x0)
            return paramValues.get(0x0);

        return null;
    }
}
